Repository navigation
fix(storage): stop concurrent CreateCacheEntry calls from reserving the same entry twice - #283
Merged
Merged
Conversation
Concurrent CreateCacheEntry calls for the same key, version, scope and repo could both pass the existence check and both reserve the entry. A unique index on a SHA-256 hash of the tuple now rejects the loser, which maps to AlreadyExistsError. The hash keeps the key under MySQL's 3072-byte limit. The migration drops older duplicate uploads, keeping the newest. Closes #281 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
📝 Summary
Two
CreateCacheEntrycalls for the same key, version, scope and repo that arrived at the same moment could both pass the existence check and both get a signed upload URL. Parallel BuildKit jobs writing the same key are the likeliest to hit this. Now the database enforces one Upload per reservation, so exactly one caller wins and the rest get 409already_exists.A unique index on
(key, version, scope, repoId)fails on MySQL (Specified key was too long; max key length is 3072 bytes), so the index covers a fixed-width hash of the tuple instead.completeUploadalso looks the upload up by that hash: on MySQL's case-insensitive collation, finishingfoocould otherwise pick up the upload forFoo. The migration deletes older duplicate uploads, keeps the newest per reservation, and leaves the dropped parts forcleanup:orphaned-storageto reclaim.✅ Verification
storage.createUpload(sameReservation)calls → 10 succeed on SQLite, 6 on Postgres. On MySQL, completingfoodeletes the in-progressFooupload.After: exactly 1 succeeds and 9 throw
AlreadyExistsError. CompletingfooleavesFooalone.vitestsuite passes on SQLite, Postgres and MySQL (filesystem storage), along withtype-check,lintandbuild.$6that held duplicate uploads. Only the newest row per reservation survived, with its hash filled in.The race test calls
Storage.createUploaddirectly. Concurrent HTTP requests arrived too staggered to reproduce the race. The 409 mapping is still covered by the existing #277 test.🔍 Reviews
Not fixed:
CONTEXT.md. It's the issue's own term, and theReservationtype documents it.🔗 Issues
Closes #281
💥 Merge danger
Door: one-way
The migration deletes duplicate in-progress upload rows. Those uploads were already conflicting, and their parts get reclaimed by orphaned-storage cleanup.
Blast Radius: uploads
During a rolling deploy, an upload created on an old replica and finalized on a new one is not found, because its hash is NULL. That affects only uploads in flight during the rollout.
completeUpload'scache_entrieslookup still compareskeycase-insensitively on MySQL. Completingfoowhile aFooentry exists repointsFoo. Worth a separate issue.🤖 Generated with Claude Code